package info.helper;

/**
 * Helper KLasse für alles was mit Mathe zu tun hat
 *
 * @author Martin
 */
public class MMath {

    public static int fibonacci(int anz) {
        if (anz <= 0) {
            return 0;
        } else if (anz == 1) {
            return 1;
        } else {
            return fibonacci(anz - 2) + fibonacci(anz - 1);
        }
    }

    public static int fibonacciIterativ(int n) {
        if (n <= 0) {
            return 0;
        } else if (n == 1) {
            return 1;
        } else {
            int a = 0;
            int b=1;
            int i=2;
            while (i <= n) {
                int aa = b;
                int bb=a+b;
                a = aa;
                b = bb;
                i++;
            }
            return b;
        }
    }

    public static int zufallZwischen(int unten, int oben) {
        return (int) (Math.random() * (oben - unten) + unten);
    }
    
    
    public static int max(int[] zahlen) {
        int max = 0;
        for (int i = 0; i < zahlen.length; i++) {
            if (zahlen[i] > max) {
                max = zahlen[i];
            }
        }
        return max;
    }
    
    public static double max(double[] zahlen) {
        double max = 0;
        for (int i = 0; i < zahlen.length; i++) {
            if (zahlen[i] > max) {
                max = zahlen[i];
            }
        }
        return max;
    }

    public static int getBiggestElementIndex(int[] array) {
        if (array == null) {
            throw new IllegalArgumentException("array == null!");
        }
        if (array.length == 0) {
            return -1;
        }
        int curMax = array[0];
        int curIndex = 0;

        for (int i = 1; i < array.length; i++) {
            if (array[i] > curMax) {
                curMax = array[i];
                curIndex = i;
            }
        }
        return curIndex;
    }
    
    public static int getSmallestElementIndex(int[] array) {
        if (array == null) {
            throw new IllegalArgumentException("array == null!");
        }
        if (array.length == 0) {
            return -1;
        }
        int curMin = array[0];
        int curIndex = 0;

        for (int i = 1; i < array.length; i++) {
            if (array[i] < curMin) {
                curMin = array[i];
                curIndex = i;
            }
        }
        return curIndex;
    }
}
